package com.common.zrd.validation;

import com.common.zrd.validation.errorcode.ErrorCode;
import com.common.zrd.validation.exception.FrameworkValidationException;

import java.util.Collection;
import java.util.Map;

/**
 * author: zrd
 * @Date: 2019/11/4 17:08
 * @Description:
 */
public class CommonValidation {

    public CommonValidation() {
    }

    public static void fail(ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        makeError(errorCode, args);
    }

    public static void notNull(Object input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input != null) {
            makeError(errorCode, args);
        }

    }

    public static void isNull(Object input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input == null) {
            makeError(errorCode, args);
        }

    }

    public static void notEmpty(String input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (FrameworkStringUtils.isNotEmpty(input)) {
            makeError(errorCode, args);
        }

    }

    public static void isEmpty(String input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (FrameworkStringUtils.isEmpty(input)) {
            makeError(errorCode, args);
        }

    }

    public static <E> void isEmpty(Collection<E> input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input == null || input.isEmpty()) {
            makeError(errorCode, args);
        }

    }

    public static <E> void notEmpty(Collection<E> input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input != null && !input.isEmpty()) {
            makeError(errorCode, args);
        }

    }

    public static <K, V> void isEmpty(Map<K, V> input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input == null || input.isEmpty()) {
            makeError(errorCode, args);
        }

    }

    public static <K, V> void notEmpty(Map<K, V> input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input != null && !input.isEmpty()) {
            makeError(errorCode, args);
        }

    }

    public static void zero(int input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input == 0) {
            makeError(errorCode, args);
        }

    }

    public static void notZero(int input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input != 0) {
            makeError(errorCode, args);
        }

    }

    public static void notDecimalDigits(String decimal, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (!FrameworkStringUtils.isDecimalDigits(decimal)) {
            makeError(errorCode, args);
        }

    }

    public static void isTrue(boolean input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input) {
            makeError(errorCode, args);
        }

    }

    public static void isFalse(boolean input, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (!input) {
            makeError(errorCode, args);
        }

    }

    public static void equals(String input, String another, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (FrameworkStringUtils.equals(input, another)) {
            makeError(errorCode, args);
        }

    }

    public static void notEquals(Object input, Object another, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (input == null || !input.equals(another)) {
            makeError(errorCode, args);
        }

    }

    public static void notEquals(String input, String another, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (!FrameworkStringUtils.equals(input, another)) {
            makeError(errorCode, args);
        }

    }

    public static void equalsIgnoreCase(String input, String another, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (FrameworkStringUtils.equalsIgnoreCase(input, another)) {
            makeError(errorCode, args);
        }

    }

    public static void notEqualsIgnoreCase(String input, String another, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (!FrameworkStringUtils.equalsIgnoreCase(input, another)) {
            makeError(errorCode, args);
        }

    }

    public static void in(String value, String[] values, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (FrameworkStringUtils.isContains(values, value)) {
            makeError(errorCode, args);
        }

    }

    public static void notin(String value, String[] values, ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        if (!FrameworkStringUtils.isContains(values, value)) {
            makeError(errorCode, args);
        }

    }


    private static void makeError(ErrorCode errorCode, Object... args) throws FrameworkValidationException {
        FrameworkValidationException e = new FrameworkValidationException(errorCode.getErrorCode(), errorCode.toString(args));
        String dumpLine = dumpLine(e);

            System.err.println(dumpLine);

        throw e;
    }

    private static String dumpLine(FrameworkValidationException e) {
        return dumpLine(e, 2);
    }

    private static String dumpLine(FrameworkValidationException e, int dept) {
        StackTraceElement traceElements = e.getStackTrace()[dept];
        return e.getLocalizedMessage() + " at " + traceElements.getClassName() + "." + traceElements.getMethodName() + "(" + traceElements.getFileName() + ":" + traceElements.getLineNumber() + ")";
    }
}
